package com.work.dao.impl;

import com.work.bean.Notice;
import com.work.dao.BaseDao;
import com.work.dao.NoticeDao;

import java.util.List;

public class NoticeDaoImpl extends BaseDao implements NoticeDao {
    public List<Notice> queryNotice() {
        String sql="SELECT * FROM t_notice ";
        return queryForList(Notice.class,sql);
    }

    public Notice queryNoticeById(Integer id) {
        String sql = "SELECT * FROM t_notice WHERE id = ? ";
        return queryForOne(Notice.class,sql,id);
    }


    public int saveNotice(Notice notice) {
        String sql = "INSERT INTO `t_notice` (`title`, `content`, `end_time`) VALUES (?, ?, ?)";
        return update(sql,notice.getTitle(),notice.getContent(),notice.getEnd_time());
    }

    public int updateNotice(Notice notice) {
        String sql = "UPDATE t_notice SET `title`=?,`content`=?,`end_time`=? WHERE `id` = ?";
        return update(sql,notice.getTitle(),notice.getContent(),notice.getEnd_time(),notice.getId());
    }

    public int deleteNotice(Integer id) {
        String sql = "DELETE FROM `t_notice` WHERE `id` = ?";
        return update(sql, id);
    }

    @Override
    public Integer queryForPageTotalCount() {
        String sql = "select count(*) from `t_notice`";
        Number count = (Number) queryForSingleValue(sql);
        return count.intValue();
    }

    @Override
    public List<Notice> queryForPageItems(int begin, int pageSize) {
        String sql = "SELECT  `id` , `title` , `content` , `end_time` FROM `t_notice` LIMIT ?,?";
        return queryForList(Notice.class, sql, begin, pageSize);
    }

    @Override
    public Integer queryForPageTotalCountByPrice(int min, int max) {
        String sql = "select count(*) from t_notice where price between ? and ?";
        Number count = (Number) queryForSingleValue(sql,min,max);
        return count.intValue();
    }

    @Override
    public List<Notice> queryForPageItemsByPrice(int begin, int pageSize, int min, int max) {
        String sql = "SELECT  `id` , `title` , `content` , `end_time` " +
                "select count(*) from t_notice where price between ? and ? order by price limit ?,?";
        return queryForList(Notice.class,sql,min,max,begin,pageSize);
    }
}
